import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():

    escort_back = DeclareLaunchArgument(
        name="turtle_back", default_value="turtle_back")

    master = Node(
        package='turtlesim',
        executable='turtlesim_node',
        name="turtle1"
    )

    spawn_back = Node(
        package='cpp05_exercise',
        executable='c01_spawn',
        name="spawn_back",
        parameters=[{"x": 2.0, "y": 5.0,
                     "turtle_name": LaunchConfiguration("turtle_back")}]
    )

   # 动态广播
    turtle1_world = Node(
        package='cpp05_exercise',
        executable='c02_broadcaster',
        name='turtle1_world'
    )

    back_world = Node(
        package='cpp05_exercise',
        executable='c02_broadcaster',
        name='back_world',
        parameters=[{'turtle': LaunchConfiguration("turtle_back")}]
    )

    # 静态广播
    escort_goal_back = Node(
        package='tf2_ros',
        executable='static_transform_publisher',
        name='escort_goal_back',
        arguments=["--frame-id", "turtle1",
                   "--child-frame-id", "escort_goal_back", "--x", "-1.5"]
    )

    # 监听
    listener = Node(
        package='cpp05_exercise',
        executable='c03_listener',
        parameters=[{'target_frame': LaunchConfiguration(
            "turtle_back"), "source_frame": "escort_goal_back"}]
    )

    return LaunchDescription([escort_back, master, spawn_back, back_world, turtle1_world, listener, escort_goal_back])
